package com.css.zfzx.data.common;

import com.css.zfzx.data.config.ProvinceCityAreaCode;
import com.css.zfzx.data.primary.base.division.repository.entity.DivisionEntity;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class ProvinceCityAreaUtil {
    private static final List<ProvinceCityAreaCode> pcaList = new ArrayList<>();

    /**
     * 将所有省市县放在同一对象中
     *
     * @return
     */
    public static void initProvinceCityArea(List<DivisionEntity> divisionlist) {
        //查询数据库
        for (DivisionEntity division : divisionlist) {
            ProvinceCityAreaCode provinceCityAreaCode = new ProvinceCityAreaCode();
            provinceCityAreaCode.setCode(division.getDivisionCode());
            String[] split = division.getAllParentId().split(",");
            if (split.length == 2) {
                //省
                provinceCityAreaCode.setProvince(division.getDivisionName());
            } else if (split.length == 3) {
                //市
                provinceCityAreaCode.setProvince(getCityName(split[1], divisionlist));
                provinceCityAreaCode.setCity(division.getDivisionName());
            } else if (split.length == 4) {
                //县
                provinceCityAreaCode.setProvince(getCityName(split[1], divisionlist));
                provinceCityAreaCode.setCity(getCityName(split[2], divisionlist));
                provinceCityAreaCode.setArea(division.getDivisionName());
            }
            pcaList.add(provinceCityAreaCode);
        }
    }
    public static String getCityName(String id, List<DivisionEntity> list) {
        for (DivisionEntity divisionEntity : list) {//id ==
            if (id.equals(divisionEntity.getDivisionId())) {
                return divisionEntity.getDivisionName();
            }
        }
        return null;
    }

    public static String getCode(String provinceName, String cityName, String areaName) {
        String code = null;
        for (ProvinceCityAreaCode pca : pcaList) {
            // 四个直辖市只有两级需要单独处理
            if (provinceName.contains("北京") || provinceName.contains("重庆") || provinceName.contains("上海") || provinceName.contains("天津")) {
                if (StringUtils.isEmpty(areaName)||"null".equals(areaName)) {
                    if (provinceName.equals(pca.getProvince())) {
                        code= pca.getCode();
                        break;
                    }
                } else {
                    if (provinceName.equals(pca.getProvince()) && areaName.equals(pca.getCity())) {
                        code= pca.getCode();
                        break;
                    }
                }
            } else {//非直辖市的都为三级
                if (StringUtils.isEmpty(cityName)||"null".equals(cityName)) {
                    if (provinceName.equals(pca.getProvince())) {
                        code = pca.getCode();
                        break;
                    }
                }
                if (StringUtils.isNotEmpty(cityName)&&!"null".equals(cityName)&&("null".equals(areaName) || StringUtils.isEmpty(areaName))) {
                    if (provinceName.equals(pca.getProvince()) && cityName.equals(pca.getCity())) {
                        code = pca.getCode();
                        break;
                    }
                }
                if (StringUtils.isNotEmpty(areaName)&&!"null".equals(areaName)) {
                    if (provinceName.equals(pca.getProvince()) && cityName.equals(pca.getCity()) && areaName.equals(pca.getArea())) {
                        code = pca.getCode();
                        break;
                    }
                }
            }
        }
        return code;
    }
}
